#!/usr/bin/perl -w 

use strict;

my ($album, $artist, $price); 
my $total = 0;
my $variable;
my $date = system("/usr/bin/date +%Y-%m-%d");

format =
  @<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<< $@###.##
  dotize(17, $album), dotize(16, $artist), $price
.

close STDOUT;
open STDOUT, '>', \$variable or die "Can't open STDOUT: $!";

while (<STDIN>) {
   chomp;
   ($album, $artist, $price) = split(/!/);

   $album = "" unless defined($album);
   $artist = "" unless defined($artist);
   $price = 0 unless defined($price);
   $total = $total + $price;
   write();
}

doEmail($variable);

sub doEmail {
    my ($content) = @_;

    return unless $content;

    my $sendmail = "/usr/sbin/sendmail -t";
    my $reply_to = "From: yanlin001\@yahoo.com\n";
    my $send_to = "To: yanlin001\@yahoo.com\n";
    my $subject = "Subject: Anywhere Client Side Daily Latency Report: $date\n";
    
    open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
    print SENDMAIL $reply_to;
    print SENDMAIL $subject;
    print SENDMAIL $send_to;
    print SENDMAIL "Content-type: text/plain\n\n";
    print SENDMAIL $content;
    print "Confirmation of your submission will be emailed to you.\n";
    close (SENDMAIL);
}

sub dotize {
    my ($width, $string) = @_;
    if (length($string) > $width) {
        return(substr($string, 0, $width - 3) . "...");
    } else {
        return($string);
    }
}

